package com.axend.aerosense.dev.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.axend.aerosense.common.router.RouterActivityPath;
import com.axend.aerosense.dev.impl.IFirstFragment;
import com.gyf.immersionbar.ImmersionBar;
import com.axend.aerosense.base.activity.MvvmBaseActivity;
import com.axend.aerosense.base.viewmodel.BaseViewModel;
import com.axend.aerosense.common.adapter.ScreenAutoAdapter;
import com.axend.aerosense.common.base.ITitle;
import com.axend.aerosense.common.util.FragmentUtil;
import com.axend.aerosense.dev.R;
import com.axend.aerosense.dev.databinding.DevActivityBindRoomBinding;
import com.axend.aerosense.dev.fragment.DevBindRoomFragment;

/**
 * @author ChenHongWei
 * @date :2021/1/12 10:19
 */

@Route(path = RouterActivityPath.Dev.PAGE_DEV_BIND_ROOM)
public class DevBindRoomActivity extends MvvmBaseActivity<DevActivityBindRoomBinding, BaseViewModel>
        implements IFirstFragment, ITitle {
    public static final int BING_REQUEST_CODE = 1818;
    public static final int BING_RESULT_CODE = 23;
    private DevBindRoomFragment firstFragment;
    private FragmentTransaction fragmentTransaction;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        ScreenAutoAdapter.match(this, 375.0f);
        super.onCreate(savedInstanceState);
        ImmersionBar.with(this)
                .statusBarColor(android.R.color.white)
                .fitsSystemWindows(true)
                .statusBarDarkFont(true)
                .autoDarkModeEnable(true).init();
        setSupportActionBar(viewDataBinding.devCustomToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左侧添加一个默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportFragmentManager().addOnBackStackChangedListener(getListener());

        firstFragment = DevBindRoomFragment.getInstance();
        FragmentUtil.add(this, viewDataBinding.devContent.getId(), firstFragment, null, false);

//        FragmentUtils.add(getSupportFragmentManager(),
//                firstFragment,
//                viewDataBinding.devContent.getId(),
//                firstFragment.getClass().getSimpleName(),
//                false,
//                com.axend.aerosense.common.R.anim.common_in_from_left,
//                com.axend.aerosense.common.R.anim.common_out_to_right,
//                com.axend.aerosense.common.R.anim.common_in_from_left,
//                com.axend.aerosense.common.R.anim.common_out_to_right);

//        viewDataBinding.devCustomToolbar.setMySettingText(getString(R.string.common_complete));
//        viewDataBinding.devCustomToolbar.setRightTitleVisibility(View.VISIBLE);
//        viewDataBinding.devCustomToolbar.setSettingTextOnClickListener(v->{
//            firstFragment.complete();
//        });
        isShowRightText(true);
    }

    /** 是否显示右边文字 */
    public void isShowRightText(boolean b){
        if (b){
            viewDataBinding.devRightText.setText(getString(R.string.common_complete));
            viewDataBinding.devRightText.setVisibility(View.VISIBLE);
            int width = (int) (getWindowManager().getDefaultDisplay().getWidth() * 0.62f);
            viewDataBinding.devTitle.setMaxWidth(width);
            viewDataBinding.devTitle.setSingleLine(true);
            viewDataBinding.devTitle.setEllipsize(TextUtils.TruncateAt.valueOf("END"));
            viewDataBinding.devRightText.setOnClickListener(v->{
                firstFragment.complete();
            });
        }else {
            viewDataBinding.devRightText.setVisibility(View.GONE);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==android.R.id.home){
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        if(getSupportFragmentManager().getBackStackEntryCount() == 0){
            finish();
            return;
        }
        super.onBackPressed();
    }
    @Override
    public void setTitle(String title){
        viewDataBinding.devTitle.setText(title);
    }
    private FragmentManager.OnBackStackChangedListener getListener() {
        FragmentManager.OnBackStackChangedListener result = new FragmentManager.OnBackStackChangedListener() {
            public void onBackStackChanged() {
                FragmentManager manager = getSupportFragmentManager();
                if (manager != null) {
                    Fragment currFrag = (Fragment) manager.findFragmentById(getContentId());
                    currFrag.onResume();
                }
            }
        };

        return result;
    }
    public int getContentId() {
        return viewDataBinding.devContent.getId();
    }

    @Override
    protected BaseViewModel getViewModel() {
        return null;
    }

    @Override
    protected int getBindingVariable() {
        return 0;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dev_activity_bind_room;
    }

    @Override
    protected void onRetryBtnClick() {

    }

    @Override
    public Fragment getFirstFragment() {
        return firstFragment;
    }

}
